package com.mh.zookeeper.api;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

import static com.mh.zookeeper.PropertiesUtils.getZookeeperConnectStr;

/**
 * @author MH
 * @since 2021-12-21
 */
public class CreateSession implements Watcher {

    private static CountDownLatch countDownLatch = new CountDownLatch(1);


    public static void main(String[] args) throws IOException, InterruptedException {

        ZooKeeper zooKeeper = new ZooKeeper(getZookeeperConnectStr(), 5000, new CreateSession());
        System.out.println(zooKeeper.getState());
        countDownLatch.await();
        System.out.println("客户端与服务端会话真正建立了");

    }


    public void process(WatchedEvent watchedEvent) {
        if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {
            System.out.println("process方法执行了...");
            countDownLatch.countDown();
        }
    }
}
